import*as e from"../../../core/common/common.js";import*as t from"../../../core/platform/platform.js";import*as i from"../../../core/sdk/sdk.js";import*as r from"../models/models.js";import*as a from"../util/util.js";class n extends Event{static eventName="selectorpicked";data;constructor(e){super(n.eventName,{bubbles:!0,composed:!0}),this.data=e}}class s extends Event{static eventName="requestselectorattribute";send;constructor(e){super(s.eventName,{bubbles:!0,composed:!0}),this.send=e}}class o{static get#e(){return i.TargetManager.TargetManager.instance()}#t;#i;#r=new e.Mutex.Mutex;active=!1;constructor(e){this.#t=e}start=()=>this.#r.run((async()=>{this.active||(this.active=!0,this.#i=await new Promise(((e,t)=>{const i=setTimeout(t,1e3);this.#t.dispatchEvent(new s((t=>{clearTimeout(i),e(t)})))})),o.#e.observeTargets(this),this.#t.requestUpdate())}));stop=()=>this.#r.run((async()=>{this.active&&(this.active=!1,o.#e.unobserveTargets(this),o.#e.targets().map(this.targetRemoved.bind(this)),this.#i=void 0,this.#t.requestUpdate())}));toggle=()=>this.active?this.stop():this.start();#a=new Map;targetAdded(t){if(t.type()!==i.Target.Type.Frame)return;let r=this.#a.get(t);r||(r=new e.Mutex.Mutex,this.#a.set(t,r)),r.run((async()=>{await this.#n(t),await this.#s(t)}))}targetRemoved(e){const t=this.#a.get(e);t&&t.run((async()=>{try{await this.#o(e),await this.#c(e)}catch{}}))}#d=e=>{if("captureSelectors"!==e.data.name)return;const t=e.data.executionContextId,a=i.TargetManager.TargetManager.instance().targets(),s=r.SDKUtils.findTargetByExecutionContext(a,t),o=r.SDKUtils.findFrameIdByExecutionContext(a,t);if(!s||!o)throw new Error(`No execution context found for the binding call + ${JSON.stringify(e.data)}`);const c=s.model(i.ResourceTreeModel.ResourceTreeModel);if(!c)throw new Error(`ResourceTreeModel instance is missing for the target: ${s.id()}`);const d=c.frameForId(o);if(!d)throw new Error("Frame is not found");this.#t.dispatchEvent(new n({...JSON.parse(e.data.payload),...r.SDKUtils.getTargetFrameContext(s,d)})),this.stop()};#l=new Map;async#s(e){const t=`${await a.InjectedScript.get()};DevToolsRecorder.startSelectorPicker({getAccessibleName, getAccessibleRole}, ${JSON.stringify(this.#i?this.#i:void 0)}, ${a.isDebugBuild})`,[{identifier:i}]=await Promise.all([e.pageAgent().invoke_addScriptToEvaluateOnNewDocument({source:t,worldName:a.DEVTOOLS_RECORDER_WORLD_NAME,includeCommandLineAPI:!0}),r.SDKUtils.evaluateInAllFrames(a.DEVTOOLS_RECORDER_WORLD_NAME,e,t)]);this.#l.set(e,i)}async#o(e){const i=this.#l.get(e);t.assertNotNullOrUndefined(i),this.#l.delete(e),await e.pageAgent().invoke_removeScriptToEvaluateOnNewDocument({identifier:i});await r.SDKUtils.evaluateInAllFrames(a.DEVTOOLS_RECORDER_WORLD_NAME,e,"DevToolsRecorder.stopSelectorPicker()")}async#n(e){const r=e.model(i.RuntimeModel.RuntimeModel);t.assertNotNullOrUndefined(r),r.addEventListener(i.RuntimeModel.Events.BindingCalled,this.#d),await r.addBinding({name:"captureSelectors",executionContextName:a.DEVTOOLS_RECORDER_WORLD_NAME})}async#c(e){await e.runtimeAgent().invoke_removeBinding({name:"captureSelectors"});const r=e.model(i.RuntimeModel.RuntimeModel);t.assertNotNullOrUndefined(r),r.removeEventListener(i.RuntimeModel.Events.BindingCalled,this.#d)}}var c=Object.freeze({__proto__:null,SelectorPickedEvent:n,RequestSelectorAttributeEvent:s,SelectorPicker:o});export{c as SelectorPicker};
